/*
 * Fight - game loop for the battle phase
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash.events.TimerEvent;
import flash.Lib;
#end

import lib3f.LCG;

class Fight
{
	public var clock : Clock;
	public var creepclock : Int;
	public var wave : Int;
	public var killsthisround : Int;
	public var finished : Bool;
	
	public function new()
	{
		
		clock = new Clock(20);
		creepclock = 0;
		wave = 1;
		killsthisround = 0;

	}
	
	static public function start()
	{

		Game.cur.fight.finished = false;

		Gameplay.screenFight();

		FortressManager.calcshores();

		Game.cur.fight.killsthisround = 0;
		
		Game.cur.fight.clock = new Clock(20);
		Game.cur.fight.creepclock = 0;
		
		#if flash9
		var pop : PopTart;
		#end
		
		switch(Game.cur.fight.wave)
		{
		
		case(1):
		{
			#if flash9
			pop = new PopTart(["1ST WAVE"]);
			#end
			new Creep(1,Game.cur.creepman.entrypoints[0].x,
						Game.cur.creepman.entrypoints[0].y,0);
		}
		case(2): 
		{
			#if flash9
			pop = new PopTart(["2ND WAVE"]);
			#end
			new Creep(1,Game.cur.creepman.entrypoints[1].x,
						Game.cur.creepman.entrypoints[1].y,1);
			new Creep(1,Game.cur.creepman.entrypoints[0].x,
						Game.cur.creepman.entrypoints[0].y,0);
		}
		case(3):
		{
			#if flash9
			pop = new PopTart(["3RD WAVE"]);
			#end
			var rand : Int = LCG.randInt(1);
			new Creep(1,Game.cur.creepman.entrypoints[2].x,
						Game.cur.creepman.entrypoints[2].y,2);
			new Creep(1,Game.cur.creepman.entrypoints[rand].x,
						Game.cur.creepman.entrypoints[rand].y,rand);
		}
		default:
		{
			#if flash9
			if (Game.cur.fight.wave==10)
				pop = new PopTart(["**FINAL WAVE**"])
			else
				pop = new PopTart([Game.cur.fight.wave,"TH WAVE"]);
			#end
			for (n in 0...(Game.cur.fight.wave)*2)
			{
				var rand : Int = LCG.randInt(2);
				new Creep(1,Game.cur.creepman.entrypoints[rand].x,
							Game.cur.creepman.entrypoints[rand].y,rand);
			}
		}
		
		}

		#if flash9
		pop.waittime = 90;
		#end
			
		ComponentManager.add(Shooter);
		ComponentManager.add(Projectile);
	}	

	static public function runstep() : Int
	{
		
		for (c in Game.cur.creepman.allcreeps)
		{
			c.move();
		}

		if(Game.cur.creepman.allcreeps.length<1)
			Game.cur.fight.clock.count-=29;

		Game.cur.fight.clock.up();
		
		if (testforfailure())
			return -1;
			
		if (Game.cur.fight.clock.count<=0)
			return 1;
		
		return 0;
		
	}

	static public function testforfailure() : Bool
	{
		var overcount = 0;
		
		for (n in MapGen.castles)
		{
			if (n.fort==null || !n.fort.iscastle)
			{
				overcount++;
			}
		}
		
		if (overcount>=3) // out of castles, game over
		{
			return true;
		}
		else
		{
			return false;
		}		
	}

	static public function end()
	{
		ComponentManager.remove(Projectile);
		ComponentManager.remove(Shooter);
		Game.cur.fight.finished = true;

		if (testforfailure())
			ComponentManager.add(WaveInterstitial);
		else
			ComponentManager.add(PlaceWalls);
	}
	
}
